package com.change.copy.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;


@Component
public class SpringContextUtil implements ApplicationContextAware, EnvironmentAware {
    private static ApplicationContext context;

    private static Environment environment;

    public SpringContextUtil() {
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        context = ctx;
    }

    public static <T> T getBean(Class<T> type) {
        return context.getBean(type);
    }

    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }

    public static String getStringBean(String beanName) {
        return (String) context.getBean(beanName, String.class);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    public static String getValue(String key) {
        return environment.getProperty(key);
    }
}